//
//  SinaNavigationController.swift
//  SinaWeibo
//
//  Created by 王朝阳 on 2016/10/23.
//  Copyright © 2016年 wangzhaoyang. All rights reserved.
//

import UIKit

class SinaNavigationController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        //此方法是为了解决自定义导航控制器之后,不能向右侧滑的问题
        interactivePopGestureRecognizer?.delegate = self
        
    }
    
    //MARK: - 点击事件
    @objc private func backClick() {
        popViewController(animated: true)
    }
    //MARK: - 重写父类的 push 方法
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        var title: String
        //因为首页消息发现我都有按钮所以
        if childViewControllers.count > 0 {
            title = "返回"
            if childViewControllers.count == 1 {
                title = childViewControllers.first?.title ?? ""
            }
            //设置导航条左侧的按钮
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(setHighlightedImgName: "navigationbar_back_withtext", title: title, target: self, action: #selector(backClick))
            
        }
        super.pushViewController(viewController, animated: true)
    }
    
    //如果是首页的时候,如果用户继续向右侧滑,则会造成点击 push 不能进行 push 的问题
    
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        
        //如果 count == 1,就代表是在首页,不接受用户的拖拽手势
        return childViewControllers.count != 1
    }

}
